package com.example.ywj.http.http;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import com.example.ywj.http.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Post2Activity extends AppCompatActivity {
    private static final String TAG = "Post2Activity";
    @BindView(R.id.btn_post_data)
    public Button btn_post_data;
    @BindView(R.id.lv_data)
    public ListView listView;
    public StringBuffer sb;
    List<User> list;
    JSONArray ja;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post2);
        ButterKnife.bind(this);
    }
    @OnClick({R.id.btn_post_data})
    public void onClickView(View view){
        switch (view.getId()){
            case R.id.btn_post_data:
                func();
                break;
        }
    }
    Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                 Gson gson = new Gson();
                 list = gson.fromJson(ja.toString(),new TypeToken<List<User>>(){}.getType());
                 UserAdapter userAdapter = new UserAdapter(list);
                 listView.setAdapter(userAdapter);
                 break;
            }
        }
    };
    public void func(){
        new Thread(){
            @Override
            public void run() {
                String data = getData();
                Message msg = Message.obtain();
                msg.what = 0;
                handler.sendMessage(msg);
                //Bundle b = new Bundle();
                //b.putString("data",data);
                //msg.setData(b);
                //handler.sendMessage(msg);
                //UserAdapter userAdapter = new UserAdapter(list);
                //listView.setAdapter(userAdapter);
            }
        }.start();
    }
    public String getData(){
        try{
            String path = "http://10.14.249.189:3009/getUser";
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("POST");
            String data = "";
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", data.length() + "");
            conn.setDoOutput(true);
            //输出流
            OutputStream os = conn.getOutputStream();
            os.write(data.getBytes());
            int respondCode = conn.getResponseCode();
            if(respondCode == 200){
                //输入流
                InputStream is = conn.getInputStream();
                byte[] bytes = new byte[1024];
                int len = -1;
                //初始化
                sb = new StringBuffer();
                while ((len = is.read(bytes)) != -1){
                    String str = new String(bytes,0,len);
                    sb.append(str);
                    Log.i(TAG, "getData: "+sb.toString());
                }
                JSONObject obj = new JSONObject(sb.toString());
                ja = obj.getJSONArray("data");
                //Gson gson = new Gson();
                //list = gson.fromJson(ja.toString(),new TypeToken<List<User>>(){}.getType());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ja.toString();
    }
}
